package br.com.sgmf.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

@Entity
@Table(name = "veiculo")
@NamedQueries({
		@NamedQuery(name = "Veiculo.findAll", query = "select v from Veiculo v"),
		@NamedQuery(name = "Veiculo.findByPlaca", query = "select v from Veiculo v where v.placa like :placa"),
		@NamedQuery(name = "Veiculo.findBetweenDate", query = "select v from Veiculo v join v.historicos h where h.data between :inicio and :fim") })
public class Veiculo implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	@Column(length = 7, unique = true, nullable = false)
	private String placa;

	@Column(length = 22, unique = true, nullable = false)
	private String chassi;

	@Column(length = 4, nullable = false)
	private Integer ano;

	@OneToOne
	private Modelo modelo;

	@OneToMany(fetch = FetchType.LAZY)
	@JoinColumn(name = "historico_veiculo_id")
	private List<HistoricoVeiculo> historicos;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getPlaca() {
		return placa;
	}

	public void setPlaca(String placa) {
		this.placa = placa;
	}

	public String getChassi() {
		return chassi;
	}

	public void setChassi(String chassi) {
		this.chassi = chassi;
	}

	public Integer getAno() {
		return ano;
	}

	public void setAno(Integer ano) {
		this.ano = ano;
	}

	public Modelo getModelo() {
		return modelo;
	}

	public void setModelo(Modelo modelo) {
		this.modelo = modelo;
	}

	public List<HistoricoVeiculo> getHistoricos() {
		return historicos;
	}

	public void setHistoricos(List<HistoricoVeiculo> historicos) {
		this.historicos = historicos;
	}

	@Override
	public int hashCode() {
		return HashCodeBuilder.reflectionHashCode(this);
	}

	@Override
	public boolean equals(Object obj) {
		return EqualsBuilder.reflectionEquals(this, obj);
	}

}
